package org.jeecg.modules.demo.annualkeywork.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.Constants;
import org.jeecg.modules.demo.annualkeywork.entity.KeyWorkDepartments;
import org.jeecg.modules.demo.annualkeywork.entity.TobedoneAnnualKeyWork;
import org.jeecg.modules.demo.annualkeywork.service.IKeyWorkDepartmentsService;
import org.jeecg.modules.demo.annualkeywork.service.ITobedoneAnnualKeyWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Tag(name="待办年度重点工作")
@RestController
@RequestMapping("/tobedoneAnnualKeyWork/tobedoneAnnualKeyWork")
@Slf4j
public class TobedoneAnnualKeyWorkController extends JeecgController<TobedoneAnnualKeyWork, ITobedoneAnnualKeyWorkService> {
    @Autowired
    private ITobedoneAnnualKeyWorkService tobedoneAnnualKeyWorkService;
    @Autowired
    private IKeyWorkDepartmentsService keyWorkDepartmentsService;
    /**
     * 分页列表查询
     *
     * @return
     */
    //@AutoLog(value = "待办年度重点工作-分页列表查询")
    @Operation(summary="待办年度重点工作-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<TobedoneAnnualKeyWork>> queryPageList(
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
            HttpServletRequest req) {
        /*
         * 添加判断，待办功能只有在指标列表中存在该部门为配合部门才显示
         * */
        PageHelper.startPage(pageNo, pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<TobedoneAnnualKeyWork> tobedoneAnnualKeyWorkList = tobedoneAnnualKeyWorkService.SelectTobedoneAnnualKeyWorkListByOrgId(sysUser.getOrgId());
        // 创建PageInfo对象
        System.out.println("list:"+tobedoneAnnualKeyWorkList);
        PageInfo<TobedoneAnnualKeyWork> pageInfo = new PageInfo<>(tobedoneAnnualKeyWorkList);
        // 转换为IPage对象
        IPage<TobedoneAnnualKeyWork> page = new Page<>(pageNo, pageSize, pageInfo.getTotal());
        page.setRecords(pageInfo.getList());
        return Result.OK(page);
    }

    /*
	接收任务
	* */
    @RequiresPermissions("tobedoneAnnualKeyWork:yx_tobedoneAnnualKeyWork:receiveTask")
    @Transactional//事务处理注解
    @RequestMapping(value = "/receiveTask", method = RequestMethod.POST)
    public Result<String> receiveTask(@RequestBody TobedoneAnnualKeyWork tobedoneAnnualKeyWork) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            List<TobedoneAnnualKeyWork> annualkeyworkList=tobedoneAnnualKeyWorkService.selectAnnualKeyWorkList();
        System.out.println(annualkeyworkList);
        for (TobedoneAnnualKeyWork t : annualkeyworkList) {
            if (t.getCoopDepart().equals(sysUser.getOrgId())) {
                QueryWrapper<KeyWorkDepartments> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id", tobedoneAnnualKeyWork.getId());
                KeyWorkDepartments toUpdate = keyWorkDepartmentsService.getOne(queryWrapper);
                System.out.println(toUpdate);
                tobedoneAnnualKeyWorkService.updateByKeyWorkId(toUpdate.getId());
            }
        }
        return Result.OK(Constants.RECEIVE_SUCCESS);
    }

}
